/*
 * Copyright © 2009 Gerald Isaac Schwarz
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package base;

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;

public class PlayerModifier extends JDialog {
	private JLabel nameLabel, colorLabel;
	private JTextField nameOption;
	private JButton colorOption, cancel, apply;
	private GridBagConstraints constraints;
	private Player player;
	private PlayerModifier me;
	private boolean result;
	
	private ActionListener buttonListener = new ActionListener() {

		@Override
		public void actionPerformed(ActionEvent e) {
			if (e.getActionCommand().equals(colorOption.getActionCommand())) {
				Color color = JColorChooser.showDialog(me, "Choose A Color", player.getColor());
				
				if (color != null) {
					player.setColor(color);
				}
			}
			else if (e.getActionCommand().equals(cancel.getActionCommand())) {
				setVisible(false);
			}
			else if (e.getActionCommand().equals(apply.getActionCommand())) {
				setResult(true);
				setVisible(false);
			}
		}
		
	};
	
	private CaretListener nameListener = new CaretListener() {

		@Override
		public void caretUpdate(CaretEvent e) {
			String oldName = player.getName();
			String newName = nameOption.getText();
			
			if (!newName.equals(oldName)) {
				player.setName(newName);
			}
		}
		
	};
	
	@Override
	public void setVisible(boolean visible) {
		if (visible) {
			this.setResult(false);
		}
		
		super.setVisible(visible);
	}
	
	public static Player showDialog(Window window, String title, Player player) {
		PlayerModifier playerModifier = new PlayerModifier(window, title, player);
		
		playerModifier.setVisible(true);
		
		if (playerModifier.isResult()) {
			return playerModifier.getPlayer();
		}
		else {
			return null;
		}
	}
	
	public PlayerModifier(Window window, String title, Player player) {
		super(window, title, JDialog.DEFAULT_MODALITY_TYPE);
		
		this.nameLabel = new JLabel("Name");
		this.nameOption = new JTextField();
		this.colorLabel = new JLabel("Color");
		this.colorOption = new JButton("Change Color");
		this.cancel = new JButton("Cancel");
		this.apply = new JButton("Apply");
		
		this.setPlayer(player);
		
		this.colorLabel.setLabelFor(this.colorOption);
		this.nameLabel.setLabelFor(this.nameOption);
		
		this.cancel.setActionCommand("Cancel");
		this.apply.setActionCommand("Apply");
		this.colorOption.setActionCommand("Change Color");
		
		this.nameOption.addCaretListener(this.nameListener);
		this.colorOption.addActionListener(this.buttonListener);
		this.cancel.addActionListener(this.buttonListener);
		this.apply.addActionListener(this.buttonListener);
		
		this.getContentPane().setLayout(new GridBagLayout());
		
		this.constraints = new GridBagConstraints();
		this.constraints.insets = new Insets(5, 5, 5, 5);
		this.constraints.anchor = GridBagConstraints.LINE_END;
		
		this.constraints.gridx = 0;
		this.constraints.gridy = 0;
		this.constraints.gridwidth = 1;
		this.constraints.weightx = 0;
		this.constraints.fill = GridBagConstraints.NONE;
		this.getContentPane().add(this.nameLabel, this.constraints);
		
		this.constraints.gridx = 1;
		this.constraints.gridy = 0;
		this.constraints.gridwidth = 2;
		this.constraints.weightx = 1;
		this.constraints.fill = GridBagConstraints.HORIZONTAL;
		this.getContentPane().add(this.nameOption, this.constraints);
		
		this.constraints.gridx = 0;
		this.constraints.gridy = 1;
		this.constraints.gridwidth = 1;
		this.constraints.fill = GridBagConstraints.NONE;
		this.getContentPane().add(this.colorLabel, this.constraints);
		
		this.constraints.gridx = 1;
		this.constraints.gridy = 1;
		this.constraints.gridwidth = 2;
		this.constraints.weightx = 1;
		this.constraints.fill = GridBagConstraints.HORIZONTAL;
		this.getContentPane().add(this.colorOption, this.constraints);
		
		this.constraints.gridx = 1;
		this.constraints.gridy = 2;
		this.constraints.gridwidth = 1;
		this.constraints.weightx = 0;
		this.constraints.fill = GridBagConstraints.NONE;
		this.getContentPane().add(this.cancel, this.constraints);
		
		this.constraints.gridx = 2;
		this.constraints.gridy = 2;
		this.constraints.gridwidth = 1;
		this.constraints.weightx = 0;
		this.constraints.fill = GridBagConstraints.NONE;
		this.getContentPane().add(this.apply, this.constraints);
		
		this.cancel.setMnemonic(KeyEvent.VK_C);
		this.apply.setMnemonic(KeyEvent.VK_A);
		this.colorOption.setMnemonic(KeyEvent.VK_H);
		
		this.setResizable(false);
		this.pack();
	}

	private void setResult(boolean result) {
		this.result = result;
	}

	public boolean isResult() {
		return this.result;
	}
	
	private void setPlayer(Player player) {
		this.player = player;
		this.nameOption.setText(this.player.getName());
	}
	
	public Player getPlayer() {
		return this.player;
	}
}
